<?php

namespace App\Admin\Forms;


use App\Models\Customer;
use App\Models\Followup;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Form;

class CustomerFollowupForm extends  Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $customer = Customer::query()->where('id',$input['id'])->first();
        $customer->status           = $input['status'] == 100 ? 101 : $input['status'];
        $customer->level            = $input['level'];
        $customer->last_note_time   = date('Y-m-d H:i:s');
        $customer->save();

        $note = new Followup();
        $note->user_id      = Admin::user()->id;
        $note->customer_id  = $input['id'];
        $note->call_status  = $input['call_status'];
        $note->content      = $input['call_status'] != 20 ? Followup::CALL_STATUS[$input['call_status']] : $input['content'];
        $note->save();

        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->hidden('id');
        $this->radio('status')->options(Customer::STATUS)->default(100)->required();
        $this->radio('level')->options(Customer::LEVEL)->default(100)->required();
        $this->radio('call_status','通话状态')->options(Followup::CALL_STATUS)->default(20)->required();
        $this->textarea('content','跟进内容');
    }

}
